package jschool.factorization;

import static org.junit.Assert.*;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;


public class FactorizatorTest {
	
	private final int CYCLES = 100000;
	private Factorizator factorizator;
	private ExecutorService executor;
	private Random random;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		executor = Executors.newFixedThreadPool(32);
		random = new Random();
		factorizator = new Factorizator();
		factorizator.initPrimes(10000);
	}

	@After
	public void tearDown() throws Exception {
		executor.shutdown();
		boolean terminated = executor.awaitTermination(20, TimeUnit.SECONDS);
		assertTrue("Finished all tasks", terminated);
		assertEquals(CYCLES, factorizator.getCount());
	}

	@Test
	public void test() {
		for (int i = 0; i < CYCLES; i++) {
			executor.submit(new Runnable() {
				@Override
				public void run() {
					System.out.println(factorizator.factorize(random.nextInt(1000) + 100));
				}
			});
		}
	}

}
